//	AUTHOR:			ENRICO CANTONI
//	CREATED:		DECEMBER 1, 2017
//	MODIFIED: 		NOVEMBER 7, 2022
//	DESCRIPTION:	Creates tables w/ descriptive stats for the Bologna's data
//					(Tables 1, A1, and A2).

// Set Stata version
version 16.1

********************************************************************************
********************************************************************************

*** TABLE 1: MEAN TURNOUT BY VOTER CHARACTERISTICS ***

// Samples for cols
#delimit ;
local samples_cols
	`""s_all"
	"s_all & female"
	"s_all & !female""'
;

// Samples for rows
local samples_rows
	`""year == 2004"
	"year == 2008"
	"year == 2009"
	"year == 2013"
	"inrange(age, 18, 25)"
	"inrange(age, 26, 45)"
	"inrange(age, 46, 65)"
	"age >= 66"
	"!married & !divorced & !widowed"
	"married"
	"divorced"
	"widowed"
	"anykids_0to5"
	"anykids_6to11"
	"anykids_12to17"
	"anykids_18more""'
;
#delimit cr

// Output sheet name
local sheet Table_1

// Initialize empty matrix
local n_cols = 3
local n_rows = 2
foreach s of local samples_rows {
	local ++n_rows
}
mat results = J(`n_rows', `n_cols', 0)

// Initialize col counter
local col = 1

// Load data
use ${analysis_clean}

// Loop over col samples
foreach s_col of local samples_cols {
	
	// Initialize row counter
	local row = 1

	// Loop over row samples
	foreach s_row of local samples_rows {

		// Store mean turnout
		gstats sum voted if `s_col' & `s_row', meanonly
		mat results[`row', `col'] = r(mean)

		// Increase row counter
		local ++row
	}

	// Store sample size and distinct voters
	gdistinct ID_voter if `s_col'
	mat results[`row', `col'] = r(N)
	mat results[`row'+1, `col'] = r(ndistinct)

	// Increase column counter
	local ++col
}

// Export matrix
clear
svmat results
matlist results
export_excel using ${tables_raw}, sheet(`sheet') sheetreplace
clear

********************************************************************************
********************************************************************************

*** APPENDIX TABLE A1: MARITAL STATUS BY YEAR ***

// Output sheet name
local sheet Table_A1

// Samples for panels
#delimit ;
local samples_panels
	s_all
	s_poor
	s_rich
;
#delimit cr

// Samples for rows
#delimit ;
local samples_rows
	`""(year == 2004)"
	"(year == 2008)"
	"(year == 2009)"
	"(year == 2013)""'
;
#delimit cr

// Vars for tabulation (rows)
#delimit ;
local vars
	nevermarried
	married
	divorced
	widowed
;
#delimit cr

// Initialize empty matrix
local n_cols = 1
foreach v of local vars {
	local ++n_cols
}
local n_rows = 0
foreach s of local samples_rows {
	local n_rows = `n_rows'+3
}
mat results = J(`n_rows', `n_cols', 0)

// Initialize row counter
local row = 1

// Loop over col samples
foreach s_panel of local samples_panels {

	// Load data
	use ${analysis_clean} if s_all

	// Create never-married vars
	gen nevermarried = maritalstatus == "Single":maritalstatus

	// Voters who are always in a poor family
	if "`s_panel'" == "s_poor" {
		fasterxtile temp = hh_income_mod, nquantiles(4) by(year)
		bysort ID_voter (temp): gen s_poor = temp[_N] <= 2
		drop temp
	}
	
	// Voters who are always in a rich family
	else if "`s_panel'" == "s_rich" {
		fasterxtile temp = hh_income_mod, nquantiles(4) by(year)
		bysort ID_voter (temp): gen s_rich = temp[1] >= 3
		drop temp
	}

	// Loop over years
	foreach s_row of local samples_rows {

		// Initialize col counter
		local col = 1

		// Loop over vars for tabulation
		foreach v of varlist `vars' {
			gstats sum `v' if `s_panel' & `s_row', meanonly
			mat results[`row', `col'] = r(mean)

			// Increase col counter
			local ++col
		}

		// Store N in given row/year
		mat results[`row', `n_cols'] = r(N)

		// Increase row counter
		local ++row
	}
	clear
}

// Export matrix
clear
svmat results
matlist results
export_excel using ${tables_raw}, sheet(`sheet') sheetreplace
clear

********************************************************************************
********************************************************************************

*** APPENDIX TABLE A2: MARRIAGE RATES AND FERTILITY BY ELECTION YEAR ***

// Output sheet name
local sheet Table_A2

// Samples for columns
#delimit ;
local samples_cols
	s_kids
	s_nevermarried
	s_bothspousesbornnorth
	s_onespousebornsouth
	s_bothspousesbornsouth
;
#delimit cr

// Outcome
local Y	nkids_0to18

// Samples for rows
#delimit ;
local samples_rows
	`""(year == 2004)"
	"(year == 2008)"
	"(year == 2009)"
	"(year == 2013)""'
;
#delimit cr

// Initialize empty matrix
local n_cols = 0
foreach s of local samples_cols {
	local ++n_cols
}
local n_rows = 1
foreach s of local samples_rows {
	local ++n_rows
}
mat results = J(`n_rows', `n_cols', 0)

// Initialize col counter
local col = 1

// Loop over col samples
foreach s_col of local samples_cols {

	// Load data
	use ${analysis_clean} if s_all

	// Never-married voters
	if "`s_col'" == "s_nevermarried" {
		keep if s_kids
		gegen temp = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		gen s_nevermarried = !temp
		drop temp
	}

	// Both parents born in Northern Italy and married at some point
	else if "`s_col'" == "s_bothspousesbornnorth" {
		keep if s_kids
		gegen temp = min(birth_region <= 8), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		gen s_bothspousesbornnorth = temp2 & temp3
		drop temp*
	}

	// 1+ parents born in the Center-Southern Italy and married at some point
	else if "`s_col'" == "s_onespousebornsouth" {
		keep if s_kids
		gegen temp = max(inrange(birth_region, 9, 20)), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		gen s_onespousebornsouth = temp2 & temp3
		drop temp*
	}

	// Both parents born in Center-Southern Italy and married at some point
	else if "`s_col'" == "s_bothspousesbornsouth" {
		keep if s_kids
		gegen temp = min(inrange(birth_region, 9, 20)), by(ID_hh)
		gegen temp2 = min(temp), by(ID_voter)
		gegen temp3 = max(maritalstatus != "Single":maritalstatus), by(ID_voter)
		gen s_bothspousesbornsouth = temp2 & temp3
		drop temp*
	}

	// Initialize row counter
	local row = 1

	// Loop over row samples
	foreach s_row of local samples_rows {
		gstats sum `Y' if `s_col' & `s_row', meanonly
		mat results[`row', `col'] = r(mean)

		// Increase row counter
		local ++row
	}

	// Get column total sample size
	gstats sum `Y' if `s_col', meanonly
	mat results[`row', `col'] = r(N)
	clear

	// Increase col counter
	local ++col
}

// Export matrix
clear
svmat results
matlist results
export_excel using ${tables_raw}, sheet(`sheet') sheetreplace
clear
